package com.graduation_design.modules.app.controller;

import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.AppProduct;
import com.graduation_design.service.AppProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/app")
public class ProductController extends BaseController{

    @Resource
    AppProductService appProductService;

    /**
     * 商品分类
     */
    @GetMapping("/category/{categoryId}")
    // 给定默认值--@RequestParam(defaultValue = "0")
    public Result category(@PathVariable Long categoryId, @RequestParam(defaultValue = "0") Integer sort) {
        return Result.success(appProductService.pageByCategoryId(getPage(), categoryId, sort));
    }

    /**
     * 商品详情
     */
    @GetMapping("/product/{productId}")
    public Result product(@PathVariable Long productId) {
        AppProduct product = appProductService.getWithAttrsById(productId);
        if (!product.getIsOnSale()) {
            return Result.fail("该商品已下架");
        }
        return Result.success(product);
    }
}
